package com.example.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.Admin;
import com.example.domain.User;
import com.example.result.JsonResult;
import com.example.service.AdminService;
import com.example.untils.MailUntils;
import com.example.untils.RedisContent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 *   管理员
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    //管理员登录
    @PostMapping("/login")
    public JsonResult login(@RequestBody Admin admin, HttpSession session){
        log.info(admin.getUsername()+"....."+admin.getUsername());
        if(admin.getUsername()==null){
            return new JsonResult(0,"用户名或密码错误");
        }
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,admin.getUsername());
        Admin result = adminService.getOne(queryWrapper);
        if(result==null||admin.getUsername().equals(result.getPassword())){
            return new JsonResult(0,"用户名或密码错误");
        }
        session.setAttribute("admin",result);
        return new JsonResult(1,"登录成功");
    }
    //管理员退出
    @GetMapping("/loginOut")
    public JsonResult loginout(HttpSession session){
        session.removeAttribute("admin");
        return new JsonResult(1,"");
    }
    //获取管理员信息
    @GetMapping("/getAdmin")
    public JsonResult getAdmin(HttpSession session){
        Admin admin=(Admin) session.getAttribute("admin");
        admin.setPassword("");
        return new JsonResult(1,"",admin);
    }
    //获取管理员信息
    @GetMapping("/getTest/{username}")
    public JsonResult getTest(@PathVariable String username,HttpSession session){
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,username);
        Admin result = adminService.getOne(queryWrapper);
        return new JsonResult(1,"",result);
    }
}
